﻿using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Openfree.Framework.DataAccess;

public partial class PMSWeb_CostBill : PMSBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TableName = "PMS_CostBill";
        this.Key = "BillID";
        if (!IsPostBack)
        {
            pnlGrid.Visible = true;
            string cmdText = @"select a.*,
                (select count(*) from PMS_CostBillItem where BillID=a.BillID) UserCount,
                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney>=0  and IsSelected=1) ShouFei,
                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney<0  and IsSelected=1) QianFei
                from PMS_CostBill a order by BillDate desc";
            FillGirdView(0, cmdText);
        }
        else
        {
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
        }
    }
   
    protected void FillGirdView(int currentPage, string cmdText)
    {
        if (string.IsNullOrEmpty(cmdText))
        {
            cmdText = ViewState[this.ClientID + "cmdText"].ToString();
        }
        SqlSplitPage splitPage = new SqlSplitPage(base.Connection, cmdText);
        splitPage.PageSize = 10;
        DataSet ds = splitPage.LoadDataSet(currentPage);
        //成功查询出数据后缓存
        ViewState[this.ClientID + "cmdText"] = cmdText;
        gridView.ShowFooter = true;
        gridView.NavigationData = splitPage.GetNavigationData(currentPage);
        gridView.DataSource = ds.Tables[0];
        gridView.DataBind();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        { 
            //e.Row.Cells[5].Text = FormatBoolean(e.Row.Cells[5].Text, "是", "否");
            //e.Row.Cells[6].Text = FormatBoolean(e.Row.Cells[6].Text, "是", "否");
            //e.Row.Cells[7].Text = FormatBoolean(e.Row.Cells[7].Text, "是", "否");
        }
    }

    protected void gridView_SplitPageIndexChange(object sender, CommandEventArgs e)
    {
        FillGirdView(Int32.Parse(e.CommandArgument.ToString()), ViewState[this.ClientID + "cmdText"].ToString());
    }

    protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Response.Redirect("CostBillEdit.aspx?"+ this.Key + "=" + e.CommandArgument.ToString());
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        StringBuilder cmdText = new StringBuilder(@"select a.*,
                (select count(*) from PMS_CostBillItem where BillID=a.BillID) UserCount,
                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney>=0  and IsSelected=1) ShouFei,
                (select sum(PayMoney) from PMS_CostBillItem where BillID=a.BillID and PayMoney<0  and IsSelected=1) QianFei
                from PMS_CostBill a where 1=1 ");

        if (tbSubject.Text.Trim() != string.Empty)
        {
            cmdText.Append("and Subject like '%" + tbSubject.Text.Trim().Replace("'", "") + "%' ");
        }
        if (!string.IsNullOrEmpty(tbBeginDate.Text.Trim()))
        {
            cmdText.Append(" and a.CreateDate >= CONVERT(varchar, '" + tbBeginDate.Text.Trim() + "', 120 ) ");
        }
        if (!string.IsNullOrEmpty(tbEndDate.Text.Trim()))
        {
            cmdText.Append(" and a.CreateDate <= CONVERT(varchar, '" + tbEndDate.Text.Trim() + "', 120 ) ");
        }
        cmdText.Append(" order by BillDate desc");

        FillGirdView(0, cmdText.ToString());
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("CostBillEdit.aspx");
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request[this.Key]))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        PMSAccess.RemoveDataRow(this.TableName, this.Key, Request[this.Key]);
        BaseAccess.ExecuteNonQuery(base.Connection, "delete from PMS_CostBillItem where BillID='" + Request[this.Key] + "'");

        FillGirdView(this.gridView.SplitPageIndex, string.Empty);
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Request[this.Key]))
        {
            base.Alert("请选择一条记录");
            FillGirdView(this.gridView.SplitPageIndex, string.Empty);
            return;
        }
        Response.Redirect("CostBillEdit.aspx?" + this.Key + "=" + Request[this.Key]);
    }
}
